#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import web
import time
import hashlib
import control
from lxml import etree
import sys
reload(sys)
sys.setdefaultencoding('utf-8') 
 
urls = (
'/weixin','WeixinInterface'
)
 

def _check_hash(data):
    #sha1加密算法
    signature=data.signature
    timestamp=data.timestamp
    nonce=data.nonce
    token="hsxsixsixsix" 
    
    #字典序排序
    list=[token,timestamp,nonce]
    list.sort()
    sha1=hashlib.sha1()
    map(sha1.update,list)
    hashcode=sha1.hexdigest()
    #如果是来自微信的请求，则回复True
    if hashcode == signature:
        return True
    return False


class WeixinInterface:
 
    def __init__(self):
        self.app_root = os.path.dirname(__file__)
        self.templates_root = os.path.join(self.app_root, 'templates')
        self.render = web.template.render(self.templates_root)
 
    def GET(self):
        #获取输入参数
	data = web.input()
        if _check_hash(data):
            return data.echostr

    def POST(self):        
        str_xml = web.data() #获得post来的数据
        xml = etree.fromstring(str_xml)#进行XML解析
        content=xml.find("Content").text#获得用户所输入的内容
        msgType=xml.find("MsgType").text
        fromUser=xml.find("FromUserName").text
        toUser=xml.find("ToUserName").text
        
        if str(content) == "开灯" :
            try:
                control.led_on(19)
                return self.render.reply_text(fromUser,toUser,int(time.time()),"已开灯！")
            except:
                return self.render.reply_text(fromUser,toUser,int(time.time()),"开灯出错了！垃圾代码！") 
        elif str(content) == "关灯":
            try:
                control.led_off(19)
                return self.render.reply_text(fromUser,toUser,int(time.time()),"已关灯！") 
            except:
                return self.render.reply_text(fromUser,toUser,int(time.time()),"关灯出错了！垃圾代码！") 
        if str(content) == "报警" :
            try:
                control.led_on(26)
                return self.render.reply_text(fromUser,toUser,int(time.time()),"已报警！")
            except:
                return self.render.reply_text(fromUser,toUser,int(time.time()),"报警出错了！垃圾代码！") 
        elif str(content) == "停止报警":
            try:
                control.led_off(26)
                return self.render.reply_text(fromUser,toUser,int(time.time()),"已停止！") 
            except:
                return self.render.reply_text(fromUser,toUser,int(time.time()),"关灯出错了！垃圾代码！") 
        if str(content) == "开蓝灯":
            try:
                control.led_on(13)
                return self.render.reply_text(fromUser,toUser,int(time.time()),"已开灯！")
            except:
                return self.render.reply_text(fromUser,toUser,int(time.time()),"开灯出错了！垃圾代码！") 
        elif str(content) == "关蓝灯":
            try:
                control.led_off(13)
                return self.render.reply_text(fromUser,toUser,int(time.time()),"已关灯！") 
            except:
                return self.render.reply_text(fromUser,toUser,int(time.time()),"关灯出错了！垃圾代码！") 
        elif str(content) == "清理GPIO":
            control.gpio_clean()
            return self.render.reply_text(fromUser,toUser,int(time.time()),"已清理！") 
            
        else:
            return self.render.reply_text(fromUser,toUser,int(time.time()),content) 
        

application = web.application(urls, globals())
if __name__ == "__main__":
    application.run()
